package com.wits.qso.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.VmDaoCustom;
import com.wits.qso.entity.Vm;

@Component
public class VmDaoImpl extends BaseDaoImpl<Vm, String> implements VmDaoCustom
{

	public Vm findByUserId(String vmUserId) {
		String sql = "select v.* from qso_vm v inner join qso_vm_user vu on(v.vm_id=vu.vm_id) where vu.user_id=?";
		List<Vm> list = this.findByNative( Vm.class, sql, vmUserId );
		if (list.size() > 0) {
			return list.get( 0 );
		}
		return null;
	}

	public Vm findByIp(String vmIp) {
		Vm result = null;
		
		try {
			Query query = em.createQuery( "FROM Vm o WHERE o.ip = :vmIp" );
			query.setParameter( "vmIp", vmIp );
			result = (Vm) query.getSingleResult();
		} catch (Throwable cause) {
		}

		return result;
	}

}
